package com.hzm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzm.dao.CourseDao;
import com.hzm.dao.OrderDao;
import com.hzm.dao.PayLogDao;
import com.hzm.entity.Order;
import com.hzm.service.OrderService;
import com.hzm.utils.Result;
import com.hzm.vo.CourseDetailsVo;
import com.qy137.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: qy137-java
 * @description:
 * @author: 皇子明
 * @create: 2021-10-19 19:42
 **/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao,Order> implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private PayLogDao payLogDao;
    @Override
    public Result getAllOrder(Integer page, Integer limit, Order order) {
        Page<Order> orderPage = new Page<>(page, limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(order.getMobile())){
            wrapper.eq("tor.mobile",order.getMobile());
        }
        if (StringUtils.isEmpty(order.getOrderNo())){
            wrapper.eq("tor.order_no",order.getOrderNo());
        }
        if (StringUtils.isEmpty(order.getTeacherName())){
            wrapper.like("tor.teacher_name",order.getTeacherName());
        }
        if (order.getStatus()!=null){
            wrapper.eq("tor.status",order.getStatus());
        }
        if (order.getPayType()!=null){
            wrapper.eq("tor.pay_type",order.getPayType());
        }
        if (StringUtils.isEmpty(order.getGmtCreate())){
            wrapper.gt("tor.gmt_create",order.getGmtCreate());
        }
        if (StringUtils.isEmpty(order.getGmtModified())){
            wrapper.le("tor.gmt_modified",order.getGmtModified());
        }
        if (StringUtils.isEmpty(order.getCourseTitle())){
            wrapper.like("tor.course_title",order.getCourseTitle());
        }
        Page<Order> allOrder = orderDao.getAllOrder(orderPage, wrapper);
        Result result = new Result(2000, "查询成功", allOrder);
        return result;
    }

    @Override
    public Result queryOrderAndPayLog(String id) {
       Order order= orderDao.queryOrderAndPayLog(id);
        Result result = new Result(2000, "查询成功", order);
        return result;
    }

    @Override
    public Result queryOrderByPay() {
       List<Order> orders=orderDao.queryOrderByPay();
        Result result = new Result(2000, "查询成功", orders);
        return result;
    }

    @Override
    public Result getOrderStatusByCourseIdAndHuiYuanId(String id, String HuiYuanId) {
        Order order= orderDao.getOrderStatusByCourseIdAndHuiYuanId(id,HuiYuanId);
        Result result = new Result(2000, "成功", order);
        return result;
    }

    @Override
    public Result updateStatus(String order_no) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order_no);
        Order order = new Order();
        order.setStatus(1);
        orderDao.update(order,wrapper);
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_no",order_no);
        Order order1 = orderDao.selectOne(wrapper);
        CourseDetailsVo courseDetailsVo = courseDao.queryAllCourseAndTeacherName(order1.getCourseId());

        long l = courseDetailsVo.getBuyCount() + 1;
        Course course = new Course();
        course.setBuyCount(l);
        course.setId(courseDetailsVo.getId());
        courseDao.updateById(course);
        Result result = new Result(2000, "成功", wrapper);
        return result;
    }

    @Override
    public Result updateZhifuStatus(String id, String HuiYuanId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        queryWrapper.eq("member_id",HuiYuanId);
        Order order = new Order();
        order.setStatus(1);
        orderDao.update(order,queryWrapper);
        Result result = new Result(2000, "成功", queryWrapper);
        return result;
    }

    @Override
    public Result queryCurrentOrderXinXi(String id, String HuiYuanId) {
        QueryWrapper<Order> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("course_id",id);
        orderWrapper.eq("member_id",HuiYuanId);
        Order order = orderDao.selectOne(orderWrapper);
        Result result = new Result(2000, "查询成功", order);
        return result;
    }
}













